﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BMS.Syncro.Devices.Configuration.Interfaces;

namespace BMS.Syncro.Devices.Configuration
{
    public class DataBaseConfiguration : IConfiguration
    {
        #region Properties
        private List<Entities.DeviceConfigurationEntity> _configurations;
        #endregion Properties

        #region IConfiguration Members

        public List<Entities.DeviceConfigurationEntity> Configurations
        {
            get
            {
                if (_configurations == null)
                    this.LoadConfigurations();
                return _configurations;
            }

            set
            {
                _configurations = value;
            }
        }

        /// <summary>
        /// Load the configuration stored in the database, table: [dbo.Tbl_Maquinas]
        /// </summary>
        public void LoadConfigurations()
        {
            try
            {
                _configurations = new List<Entities.DeviceConfigurationEntity>();

                foreach( var maniche in new Dao.ConfigurationDaoNHibernate().List())
                {
                    _configurations.Add(new Entities.DeviceConfigurationEntity() {Ip= maniche.IpAddress,
                        Port = maniche.IpPort.ToString(),
                        Name = maniche.MachineName
                    } );
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// Get the configuration list
        /// </summary>
        /// <returns>Configuration list with all the devices stored in the database</returns>
        public List<Entities.DeviceConfigurationEntity> GetConfigurations()
        {
            return _configurations;
        }

        #endregion
    }
}
